package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.Company;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description : SysCompany 控制器
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2018-11-18
 */
@RestController
@Api(value="/company", description="用户公司控制器")
@RequestMapping("api/company")
public class CompanyController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(CompanyController.class);

    @Autowired
    public CompanyService companyService;

    /**
     * @description : 获取分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-11-18
     */
    @RequiresPermissions("cms:company:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page(Integer pageSize, Integer pageNum) {
        Wrapper<Company> ew = new EntityWrapper<Company>();
        ew.where("1=1");

        //logger.debug("SQL:"+ew.originalSql());

        Page<Company> page = new Page<Company>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        companyService.findPage(page, ew);

        return ResultMessage.success("获取Log分页列表",page);
    }

    /**
     * @description : 获取所有用户公司信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-11-20
     */
    @RequiresPermissions("cms:company:index")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResultMessage all() {
        return ResultMessage.success("获取所有用户公司信息",companyService.findAll());
    }


    /**
     * @description : 通过id获取用户公司
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-11-20
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取用户公司信息", notes="获取用户公司信息")
    @ApiImplicitParam(name="lang",value="用户公司语言Code",required=true)
    public ResultMessage get(String lang) {
        return ResultMessage.success("获取用户公司信息成功!",companyService.selectById(lang));
    }

    /**
     * @param company
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultMessage update(Company company) {
        if(companyService.insertOrUpdate(company)) {
            return ResultMessage.success("用户公司更新成功!");
        }else{
            return ResultMessage.fail("用户公司更新失败!");
        }
    }

}
